//
//  CicleViewController.m
//  coachmanage
//
//  Created by 众拳科技 on 2018/3/6.
//  Copyright © 2018年 众拳科技. All rights reserved.
//

#import "CicleViewController.h"
#import "SMPagerTabView.h"
#import "SMPagerScrollView.h"
#import "CZQCycleChildHotVC.h"
#import "CZQCyclePublishVC.h"
#import "CZQPublishChoseView.h"
#import "CZQGoodListVC.h"
@interface CicleViewController ()
/**
 *  内容视图
 */
@property(nonatomic,weak)SMPagerScrollView *contentView;

@property(nonatomic,weak)UIView *btnView;


/**
 移动光标
 */
@property(nonatomic,weak)UIView *lineView;




@end

@implementation CicleViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = BackGroundColor;
    
    //导航栏设置多个按钮
    
    
    
    
    // 初始化所有子控制器
    [self setupChildVces];
    
    // 底部cententView
    [self setupContentView];
    
    [self createSubView];
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
}








/**
 *  初始化所有子控制器
 */
- (void)setupChildVces
{
    
    CZQCycleChildHotVC *oneChildVC = [CZQCycleChildHotVC new];
    
    [self addChildViewController:oneChildVC];
    
//    CZQCycleChildHotVC *twoChile = [CZQCycleChildHotVC new];
//
//    [self addChildViewController:twoChile];
    
    CZQGoodListVC *triChildVC = [CZQGoodListVC new];
    
    triChildVC.title = @"关注";
    
    
    [self addChildViewController:triChildVC];
    
    
}


/**
 *  底部cententView
 */
- (void)setupContentView
{
    // 不要自动调整inset
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    SMPagerScrollView *contentView = [[SMPagerScrollView alloc] init];
    contentView.frame = CGRectMake(0, 0, kScreenWidth, SCREEN_HEIGHT  - adaptNavagationBar -CZQ_TabbarHeight);
    contentView.delegate = self;
    contentView.pagingEnabled = YES;
    [self.view addSubview:contentView];
    contentView.contentSize = CGSizeMake(contentView.width * self.childViewControllers.count, contentView.height);
    self.contentView = contentView;
    for (int i = 0; i < self.childViewControllers.count; i++) {
        
        // 取出子控制器
        UIViewController *vc = self.childViewControllers[i];
        
        vc.view.frame = CGRectMake(contentView.width * i, 0, contentView.width, contentView.height);
        
        [contentView addSubview:vc.view];
    }
    
    // 添加第一个控制器的view
    //    [self scrollViewDidEndScrollingAnimation:contentView];
}

-(void)createSubView{
    
    UIView *btnView = [[UIView alloc]initWithFrame:CGRectMake(kScreenWidth/4, CZQ_StatusBarHeight, kScreenWidth/2, 44)];
    
    self.btnView = btnView;
    
    NSArray *btnArr = @[@"热门",@"关注"];
    
    for (int i = 0; i < btnArr.count; i ++) {
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [btn setTitle:btnArr[i] forState:UIControlStateNormal];
        
        
        btn.tag = 100 + i;
        
        [btn addTarget:self action:@selector(btnClick:)];
        
        
        [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        
        [btn setTitleColor:CSBlackColor forState:UIControlStateSelected];
        
        
        btn.frame = CGRectMake(i * kScreenWidth/2/btnArr.count, 0, kScreenWidth/2/btnArr.count, 44);
        
        [btnView addSubview:btn];
        
    }
    
    self.navigationItem.titleView = btnView;
    
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(kScreenWidth/4/btnArr.count - 25, 40, 50, 2)];
    
    line.backgroundColor = ButtonBackGroundColor;
    
    self.lineView = line;
    
    [btnView addSubview:line];
    
    //右侧图片 icon_nbarmore
    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"icon_camera"]
                                                                               imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(publish)];
    self.navigationItem.rightBarButtonItem = rightButtonItem;
    
    
    //发布
    UIButton *publishButton = [UIButton buttonWithType:UIButtonTypeCustom];
    publishButton.layer.cornerRadius = 25;
    publishButton.clipsToBounds = YES;
    publishButton.backgroundColor = ButtonBackGroundColor;
    [publishButton setImage:[UIImage imageNamed:@"icon_addmore"] forState:UIControlStateNormal];
    publishButton.contentEdgeInsets = UIEdgeInsetsMake(15, 15, 15, 15);
    [self.view addSubview:publishButton];
    [publishButton addTarget:self action:@selector(publish) forControlEvents:UIControlEventTouchUpInside];
    
    
    [publishButton mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.bottom.mas_equalTo(self.view).offset(-20);
        make.right.mas_equalTo(self.view).offset(-20);
        make.height.width.mas_equalTo(50);
    }];
    
    [publishButton bringSubviewToFront:self.view];
    
}



#pragma mark - <UIScrollViewDelegate>

/**
 *  在scrollview停止滑动的时候执行
 */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //[self scrollViewDidEndScrollingAnimation:scrollView];
    
    // 点击菜单按钮
    NSInteger index = scrollView.contentOffset.x / scrollView.width;
    
    
    
    UIButton *btn = [self.btnView viewWithTag:100 + index];
    
    self.lineView.centerX = btn.centerX;
    
}

#pragma mark 发布话题
-(void)publish
{
    
    CZQPublishChoseView *shareAlertView = [[CZQPublishChoseView alloc] init];
    shareAlertView.weChatBtn.tag = 0;
    shareAlertView.friendBtn.tag = 1;
    [shareAlertView.weChatBtn addTarget:self action:@selector(voice:)];
    [shareAlertView.friendBtn addTarget:self action:@selector(voice:)];
    [shareAlertView showWithAnimation];
    
    
}

-(void)voice:(UIButton *)sender
{
    
    
    CZQCyclePublishVC *publish = [CZQCyclePublishVC new];
    
    publish.isVideo = sender.tag == 0?YES:NO;
    
    [self.navigationController pushViewController:publish animated:YES];
    
}

#pragma mark 按钮点击
-(void)btnClick:(UIButton *)btn{
    
    NSInteger tag = btn.tag - 100;
    
    [self.contentView setContentOffset:CGPointMake(self.contentView.width * tag , 0) animated:YES];
    
    
    self.lineView.centerX = btn.centerX;
}









- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
